Skip to content

feat: focusSlot API and cursor lock fix - #13

Merged
yjose merged 5 commits into
mainfrom
fixes
Mar 3, 2026
Merged

feat: focusSlot API and cursor lock fix#13
yjose merged 5 commits into
mainfrom
fixes

Conversation

@yjose

@yjose yjose commented Mar 3, 2026

Copy link
Copy Markdown
Owner

What

Two improvements shipped together:

slot.focus() on SlotProps

  • Each slot now exposes a focus() method directly — no OTPInputRef needed for tappable slots.
  • Replaces the previous pattern of ref.current?.focusSlot(index) with the simpler onPress={slot.focus}.
  • Automatically suppresses the iOS clearTextOnFocus empty-string event fired after slot.focus(), so the truncated value is preserved correctly.
  • New clearTextOnFocus prop (default true) for manual control when needed.

Cursor lock fix

  • Arrow key navigation on a physical/Bluetooth keyboard could move the hidden TextInput cursor backwards, causing Backspace to delete the wrong character or do nothing.
  • Fix: selection={{ start: value.length, end: value.length }} keeps the cursor at the end on every render; onSelectionChange triggers a re-render to snap it back if it drifts.

Changes

File Change
src/use-input.tsx slot.focus on each SlotProps; suppress iOS clearTextOnFocus after slot focus; onSelectionChange handler
src/input.tsx clearTextOnFocus prop; conditional onPress clear; pointerEvents="none" on TextInput
src/types.ts focus added to SlotProps; clearTextOnFocus added to OTPInputProps
src/input.test.tsx Tests rewritten around slot.focus(); suppression behavior covered
docs/ slot.focus documented in SlotProps table; focusSlot guide merged into getting-started
example/ All Slot components are now Pressable with onPress={focus} — no wrapper needed

yjose added 5 commits March 3, 2026 15:18
When users navigate the hidden TextInput with arrow keys, the cursor
could move backwards causing Backspace to delete the wrong character
or do nothing. Lock the cursor at the end at all times using the
`selection` prop and an `onSelectionChange` handler that forces a
re-render to snap it back if it drifts.
Each slot now carries a `focus()` method that calls focusSlot internally,
eliminating the need for an OTPInputRef just to support tappable slots.

Also adds a `clearTextOnFocus` prop (default true) and automatically
suppresses the iOS native clearTextOnFocus empty-string event when
slot.focus() is used, so the truncated value is preserved correctly.

Docs consolidate the focusSlot guide into getting-started and remove
the separate guides/ section.
@yjose
yjose merged commit 051e1c6 into main Mar 3, 2026
4 checks passed
@yjose
yjose deleted the fixes branch March 3, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant